package com.beyond.SearchNearby.view;

import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.map.MKEvent;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapView;
import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.beyond.SearchNearby.R;

import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * User: 闯儿
 * Date: 13-10-12
 * Time: 下午3:46
 * To change this template use File | Settings | File Templates.
 */
public class MapActivity extends Activity {
    private BMapManager bMapManager = null;
    private static  final String  strKey = "7b856da0b9349192f6bf6151a703e24c";
    private LayoutInflater layoutInflater;
    private LocationClient locationClient;
    private MapView mapView;
    private CheckBox satelliteCheckBox;
    private CheckBox trafficCheckBox;
    private CheckBox builtInZoomControlsCheckBox;
    private MapController mapController;
    LocationListener mLocationListener = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        bMapManager = new BMapManager(this);
        boolean initResult = bMapManager.init(strKey,new MKGeneralListener() {


            @Override
            public void onGetNetworkState(int i) {
                if (i == MKEvent.ERROR_NETWORK_CONNECT)
                {
                    Toast.makeText(MapActivity.this,"您的网络连接错误",Toast.LENGTH_LONG).show();

                }
                if (i== MKEvent.ERROR_NETWORK_DATA)
                {
                    Toast.makeText(MapActivity.this,"请正确输入索引文件",Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onGetPermissionState(int i) {
                if (i == MKEvent.ERROR_PERMISSION_DENIED)
                {
                    Toast.makeText(MapActivity.this ,"请输入正确的strKey值",Toast.LENGTH_LONG).show();
                }
            }
        }) ;
        if (!initResult)
        {
           Toast.makeText(MapActivity.this,"  BapManager   初始化错误",Toast.LENGTH_LONG).show();
        }

        setContentView(R.layout.map_activity);

        layoutInflater = LayoutInflater.from(this);

        mapView = (MapView) findViewById(R.id.BDMap);
        satelliteCheckBox = (CheckBox) findViewById(R.id.map_satellite_checkBox);
        trafficCheckBox = (CheckBox) findViewById(R.id.map_traffic_checkBox);
        builtInZoomControlsCheckBox = (CheckBox) findViewById(R.id.map_BuiltInZoomControls_checkBox);

//             获取地图控制
        mapController = mapView.getController();
        mapController.setZoom(14);
//
        GeoPoint geoPoint=new GeoPoint((int) (34.25934463685013 * 1E6), (int) (108.94721031188965 * 1E6));

        mapController.setCenter(geoPoint);

    }

    public void Check1OnClick(View view)
    {

      if (satelliteCheckBox.isChecked())
      {
          mapView.setSatellite(true);
      }else
      {
          mapView.setSatellite(false);
      }
    }
    public void Check2OnClick(View view)
    {
        if (trafficCheckBox.isChecked())
        {
            mapView.setTraffic(true);
        }else
        {
            mapView.setTraffic(false);
        }
    }
    public void Check3OnClick(View view)
    {
        if (builtInZoomControlsCheckBox.isChecked())
        {
            mapView.setBuiltInZoomControls(true);
        }else
        {
            mapView.setBuiltInZoomControls(false);
        }
    }


    public void button4OnClick(View view) {
        Log.d("BaiduMapDemo", "getMyLocation");
        locationClient= new LocationClient(this);
        LocationClientOption option = new LocationClientOption();
        option.setCoorType("bd09ll");
        option.setAddrType("all");
        option.setOpenGps(true);
        option.setPriority(LocationClientOption.NetWorkFirst);
        option.setScanSpan(5000);


        locationClient.setLocOption(option);

        locationClient.registerLocationListener(new BDLocationListener() {
            @Override
            public void onReceiveLocation(BDLocation bdLocation) {
                if (bdLocation == null)
                    return ;
                Log.d("BaiduMapDemo", "onReceiveLocation address " + bdLocation.getAddrStr());

                Log.d("BaiduMapDemo", "onReceiveLocation Latitude " + bdLocation.getLatitude());
                Log.d("BaiduMapDemo", "onReceiveLocation Longitude " + bdLocation.getLongitude());

                double lng = bdLocation.getLongitude();
                double lat = bdLocation.getLatitude();
                int offset = (int) (new Random().nextFloat() * 10000);
                GeoPoint point = new GeoPoint((int) (lng * 1E6) + offset, (int) (lat * 1E6) - offset);
                mapController.setCenter(point);
//                myLocationOverlay.setMyLocation(point);

                mapView.refresh();
            }

            @Override
            public void onReceivePoi(BDLocation bdLocation) {
                Log.d("BaiduMapDemo", "onReceivePoi ");
            }
        });

        locationClient.start();
        locationClient.requestLocation();
        Toast.makeText(MapActivity.this, "正在定位……", Toast.LENGTH_SHORT).show();



    }
    public void locationOnclick(View view)
    {
        mLocationListener=   new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                //To change body of implemented methods use File | Settings | File Templates.
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
                //To change body of implemented methods use File | Settings | File Templates.
            }

            @Override
            public void onProviderEnabled(String provider) {
                //To change body of implemented methods use File | Settings | File Templates.
            }

            @Override
            public void onProviderDisabled(String provider) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        };
    }
}

